// pages/profile/coupon-list/coupon-list.js
const app = getApp()
const constant = require("../../../constant.js")
const storage = require("../../../utils/storage.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    rule: '',
    code: ''
  },

  bindClickRule: function(e) {
    this.setData({
      showModal: true,
      rule: e.currentTarget.dataset.description
    })
  },

  bindClickRedeemCode: function(e) {
    this.setData({
      codeShowModal: true,
      codeShowMessage: '礼品券兑换码',
      code: e.currentTarget.dataset.code
    })
  },

  bindClickGiftCode: function(e) {
    this.setData({
      codeShowModal: true,
      codeShowMessage: '抵用券核销码',
      code: e.currentTarget.dataset.code
    })
  },

  bindClickCancel: function() {
    this.setData({
      showModal: false,
      codeShowModal: false,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    let unionid = wx.getStorageSync(storage.keys.unionid);

    wx.request({
      url: constant.api(app.globalData.pord).url + '/profile/coupons?unionid=' + unionid,
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        if (res.data.code == "0") {
          that.setData({
            coupons: res.data.data.coupons,
            availableCoupons: res.data.data.availableCoupons,
            unavailableCoupons: res.data.data.unavailableCoupons,
            userId: res.data.data.userId
          });
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 1000
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '加载数据失败',
          icon: 'none',
          duration: 1000
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})